package th.co.exat.uap.controller;

import com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import th.co.exat.uap.model.Car;
import th.co.exat.uap.model.Stations;
import th.co.exat.uap.model.UAPPoliceData;
import th.co.exat.uap.util.date.FormateDate;
import th.co.exat.uap.util.exception.ErrorHandle;

public class UAP002Add extends HttpServlet {

    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        HttpSession session = request.getSession(true);
        RequestDispatcher rd;
        List<ErrorHandle> errors = new ArrayList<ErrorHandle>();
        try {
            /** GET PARAMETER */
            String exitStation = request.getParameter("exitStation");
            String enterStation = request.getParameter("enterStation");
            String uapDate = request.getParameter("uapDate");
            String hour = request.getParameter("hour");
            String minute = request.getParameter("minute");
            String carType = request.getParameter("carType");
            String color = request.getParameter("color");
            String brand = request.getParameter("brand");
            String plate = request.getParameter("plate");
            String province = request.getParameter("province");
            String pay = request.getParameter("pay");
            String rate = request.getParameter("rate");
            String card = request.getParameter("card");
            String emp1 = request.getParameter("emp1a");
            String emp2 = request.getParameter("emp2a");
            String emp3 = request.getParameter("emp3a");
            String polId = request.getParameter("pola");
            String len = request.getParameter("len");
            String message = "";
            /** END GET PARAMETER */
            /** STRING ENCODING  PARAMETER */
            plate = new String(plate.getBytes("iso-8859-1"), "utf-8");
            /** END STRING ENCODING  PARAMETER */
            if (enterStation.equals("")) {
                enterStation = "0";
            }

            /** SET VALUES TO UAPData */
            UAPPoliceData u = new UAPPoliceData();
            u.setUapExitStation(exitStation.trim());
            u.setUapEnterStation(enterStation.trim());
            u.setUapDate(FormateDate.convertDateTimeToDatabase(uapDate, hour, minute));
            u.setCarTypeId(carType);
            u.setColorId(color);
            u.setBrandId(brand);
            u.setPlateNo(plate);
            u.setProvinceId(province);
            u.setPay(Double.parseDouble(pay));
            u.setRate(Double.parseDouble(rate));
            u.setIcCard(0);
            u.setEmp1(emp1);
            u.setEmp2(emp2);
            u.setEmp3(emp3);
            u.setChanel(len);
            u.setPoliceId(polId);
            u.setUapCreateBy((String) session.getAttribute("uap_user_id"));
            /** END SET VALUES TO UAPData */

            if (UAPPoliceData.insertUapData(u) != 0) {
                message = "<div class=\"success-box\">บันทึกข้อมูลเรียบร้อย</div>";
            } else {
                message = "ไม่สามารถบันทึกข้อมูล";
            }

            /**
             * GET LIST DATA
             */
            List<Stations> lineList = Stations.getLineList();
            List<Car> carTypeList = Car.getCarTypeList();
            List<Car> colorList = Car.getColorList();
            List<Car> brandList = Car.getBrandList();
            List<Car> provinceList = Car.getProvinceList();
            /**
             * END GET LIST DATA
             */
            /**
             * SET ATTRIBUTE
             */
            request.setAttribute("lineList", lineList);
            request.setAttribute("carTypeList", carTypeList);
            request.setAttribute("colorList", colorList);
            request.setAttribute("brandList", brandList);
            request.setAttribute("provinceList", provinceList);
            request.setAttribute("now", FormateDate.now());
            request.setAttribute("message", message);
            /**
             * END SET ATTRBUTE
             */
            /**
             * FORWORD TO JSP PAGE
             */
            rd = request.getRequestDispatcher("uap002add.jsp");
            rd.forward(request, response);
            /**
             * END FORWORD TO JSP PAGE
             */
        } catch (Exception ex) {
            /*ErrorHandle e = new ErrorHandle();
            e.setCode("UAP002");
            e.setMessage("UAPAdd002 : " + ex);
            errors.add(e);

            request.setAttribute("errors", errors);

            rd = request.getRequestDispatcher("errors.jsp");
            rd.forward(request, response);
             *
             */

            throw new ServletException(ex);
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
